---
title: "Figure A.1"
output: 
---

# Figure A.1

# Who's to Blame? Postconflict Violence and Public Attitudes Towards Peace Agreements
# Wyer, Frank. 

#clear environment
```{r clear environment}
rm(list = ls())
```

# uncomment and set working directory to replication archive
# setwd("~/blame_replication")

# Uncomment to install packages if necessary
# install.packages("tidyverse")
# install.packages("cowplot")
# install.packages("estimatr")

#load packages
```{r}
library(tidyverse)
library(estimatr)
library(cowplot)
```

#read in data
```{r}
survey_clean <- read.csv("survey_clean.csv")
```

#create binary variables for security trajectory and political engagement
```{r generate binary variables}
survey_clean <- survey_clean %>% 
  mutate(homs_worse = ifelse(homratediff > 0, 1, 0), 
         high_engage = ifelse(engage_zscale > median(engage_zscale), 1, 0))
```

#het effects FARC presence treatment vs control
```{r het effects: farc presence}
m_farc_no <- lm_lin(formula = outcomes_zscale ~ treatment, covariates = ~ Q15 + homratediff + urbandummy + Q25 + engage_zscale + factor(regionname), se_type = "HC2", data = survey_clean %>% filter(farc_presence == 0), alpha = .05) %>% tidy() %>% filter(term == "treatmentT1" | term == "treatmentT2A" | term == "treatmentT2B") %>% mutate(farc = "Non-FARC")
m_farc_yes <- lm_lin(formula = outcomes_zscale ~ treatment, covariates = ~ Q15 + homratediff + urbandummy + Q25 + engage_zscale + factor(regionname), se_type = "HC2", data = survey_clean %>% filter(farc_presence == 1), alpha = .05) %>% tidy() %>% filter(term == "treatmentT1" | term == "treatmentT2A" | term == "treatmentT2B") %>% mutate(farc = "FARC")

het_farc_df <- rbind(m_farc_no, m_farc_yes)

het_farc_df <- het_farc_df %>% mutate(Treatment = case_when(
term == "treatmentT1" ~ "Postconflict Violence",
term == "treatmentT2B" ~ "Rebel Culpability",
term == "treatmentT2A" ~ "Govt. Culpability",
))

het_farc_df$Treatment <- factor(het_farc_df$Treatment, levels = c("Postconflict Violence", "Rebel Culpability", "Govt. Culpability"))

farc_het_plot <- ggplot(het_farc_df,  aes(x = estimate, y = farc)) +
    geom_point(position=position_dodge(.5), shape = 15) +
    geom_linerange(aes(y = farc, xmin = conf.low, xmax = conf.high), position=position_dodge(.5)) +
    labs(x = "", y = "", title = "FARC Presence") +
    theme_bw() + 
    xlim(-1.5,1.5) +
    scale_y_discrete(labels = function(x) str_wrap(x, width = 4)) +
    theme(plot.title = element_text(face = "bold", size = 10)) +
    facet_grid(Treatment ~ ., labeller = label_wrap_gen(width = 15, multi_line = TRUE))
```

#het effects high engagement treatment vs control
```{r het effects: engagement}
m_engage_low <- lm_lin(formula = outcomes_zscale ~ treatment, covariates = ~ Q15 + homratediff + urbandummy + Q25 + farc_presence + factor(regionname), se_type = "HC2", data = survey_clean %>% filter(high_engage == 0), alpha = .05) %>% tidy() %>% filter(term == "treatmentT1" | term == "treatmentT2A" | term == "treatmentT2B") %>% mutate(engagement = "Low")
m_engage_high <- lm_lin(formula = outcomes_zscale ~ treatment, covariates = ~ Q15 + homratediff + urbandummy + Q25 + farc_presence + factor(regionname), se_type = "HC2", data = survey_clean %>% filter(high_engage == 1), alpha = .05) %>% tidy() %>% filter(term == "treatmentT1" | term == "treatmentT2A" | term == "treatmentT2B") %>% mutate(engagement = "High")

het_engage_df <- rbind(m_engage_low, m_engage_high)

het_engage_df$engagement <- factor(het_engage_df$engagement, levels = c("Low", "High"))

het_engage_df <- het_engage_df %>% mutate(Treatment = case_when(
term == "treatmentT1" ~ "Postconflict Violence",
term == "treatmentT2B" ~ "Rebel Culpability",
term == "treatmentT2A" ~ "Govt. Culpability",
))

het_engage_df$Treatment <- factor(het_engage_df$Treatment, levels = c("Postconflict Violence", "Rebel Culpability", "Govt. Culpability"))

engage_het_plot <- ggplot(het_engage_df,  aes(x = estimate, y = engagement)) +
    geom_point(position=position_dodge(.5), shape = 15) +
    geom_linerange(aes(y = engagement, xmin = conf.low, xmax = conf.high), position=position_dodge(.5)) +
    labs(x = "", y = "", title = "Political Engagement") +
    theme_bw() + 
    xlim(-1.5,1.5) +
        scale_y_discrete(labels = function(x) str_wrap(x, width = 10)) +
        theme(plot.title = element_text(face = "bold", size = 10)) +
    facet_grid(Treatment ~ ., labeller = label_wrap_gen(width = 15, multi_line = TRUE))
```

#het effects left-right treatment vs control

```{r het effects df: left vs right}
m_id_right <- lm_lin(formula = outcomes_zscale ~ treatment, covariates = ~ Q15 + homratediff + urbandummy + engage_zscale + farc_presence + factor(regionname), se_type = "HC2", data = survey_clean %>% filter(Q25 > 3), alpha = .05) %>% tidy() %>% filter(term == "treatmentT1" | term == "treatmentT2A" | term == "treatmentT2B") %>% mutate(ideology = "Right")
m_id_left <- lm_lin(formula = outcomes_zscale ~ treatment, covariates = ~ Q15 + homratediff + urbandummy + engage_zscale + farc_presence + factor(regionname), se_type = "HC2", data = survey_clean %>% filter(Q25 < 4), alpha = .05) %>% tidy() %>% filter(term == "treatmentT1" | term == "treatmentT2A" | term == "treatmentT2B") %>% mutate(ideology = "Left")

het_id_df <- rbind(m_id_right, m_id_left)

het_id_df$ideology <- factor(het_id_df$ideology, levels = c("Left", "Right"))

het_id_df <- het_id_df %>% mutate(Treatment = case_when(
term == "treatmentT1" ~ "Postconflict Violence",
term == "treatmentT2B" ~ "Rebel Culpability",
term == "treatmentT2A" ~ "Govt. Culpability",
))

het_id_df$Treatment <- factor(het_id_df$Treatment, levels = c("Postconflict Violence", "Rebel Culpability", "Govt. Culpability"))

id_het_plot <- ggplot(het_id_df,  aes(x = estimate, y = ideology)) +
    geom_point(position=position_dodge(.5), shape = 15) +
    geom_linerange(aes(y = ideology, xmin = conf.low, xmax = conf.high), position=position_dodge(.5)) +
    labs(x = "", y = "", title = "Ideology") +
    theme_bw() + 
    xlim(-1.5,1.5) +
    guides(color = "none", shape = "none") +
          scale_y_discrete(labels = function(x) str_wrap(x, width = 10)) +
    theme(plot.title = element_text(face = "bold", size = 10)) +
    facet_grid(Treatment ~ ., labeller = label_wrap_gen(width = 15, multi_line = TRUE))
```

#het effects security treatment vs control

```{r}
m_sec_worse <- lm_lin(formula = outcomes_zscale ~ treatment, covariates = ~ Q15 + Q25 + urbandummy + engage_zscale + farc_presence + factor(regionname), se_type = "HC2", data = survey_clean %>% filter(homs_worse == 1), alpha = .05) %>% tidy() %>% filter(term == "treatmentT1" | term == "treatmentT2A" | term == "treatmentT2B") %>% mutate(sec_tercile = "Worsened")
m_sec_better <- lm_lin(formula = outcomes_zscale ~ treatment, covariates = ~ Q15 + Q25 + urbandummy + engage_zscale + farc_presence + factor(regionname), se_type = "HC2", data = survey_clean %>% filter(homs_worse == 0), alpha = .05) %>% tidy() %>% filter(term == "treatmentT1" | term == "treatmentT2A" | term == "treatmentT2B") %>% mutate(sec_tercile = "Improved")

het_sec_df <- rbind(m_sec_worse, m_sec_better)

het_sec_df <- het_sec_df %>% mutate(Treatment = case_when(
term == "treatmentT1" ~ "Postconflict Violence",
term == "treatmentT2B" ~ "Rebel Culpability",
term == "treatmentT2A" ~ "Govt. Culpability",
))

het_sec_df$Treatment <- factor(het_sec_df$Treatment, levels = c("Postconflict Violence", "Rebel Culpability", "Govt. Culpability"))

sec_het_plot <- ggplot(het_sec_df,  aes(x = estimate, y = sec_tercile)) +
    geom_point(position=position_dodge(.5), shape = 15) +
    geom_linerange(aes(y = sec_tercile, xmin = conf.low, xmax = conf.high), position=position_dodge(.5)) +
    labs(x = "", y = "", title = "Security Trajectory") +
    theme_bw() + 
    xlim(-1.5,1.5) +
        scale_y_discrete(labels = function(x) str_wrap(x, width = 10)) +
    theme(plot.title = element_text(face = "bold", size = 10)) +
    facet_grid(Treatment ~ ., labeller = label_wrap_gen(width = 15, multi_line = TRUE))
```

#combined plot
```{r combine plots}
het_plot_all_c <- plot_grid(farc_het_plot, sec_het_plot, id_het_plot, engage_het_plot)
```

